package com.lt.flashcard.view;

import java.util.List;

import org.zkoss.lang.OperationException;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;

import com.lt.flashcard.model.entity.Flashcard;
import com.lt.flashcard.model.entity.FlashcardSet;
import com.lt.flashcard.model.service.FlashcardSetService;
import com.lt.flashcard.model.service.Repository;

/**
 * Render the wordlist detail page
 * @author ocsen
 * @version 1.0
 */
public class WordlistDetailCardViewCtrl extends SelectorComposer<Component> {

    private static final long serialVersionUID = 1L;
    /**
     * Store current flashcard set
     */
    private FlashcardSet flashcardset = null;

    @Wire
    /**
     * The listbox that displays flashcards list
     */
    private Listbox listboxFlashcard;

    @Wire(".chkFlashcards")
    private List<Checkbox> chkFlashcards;

    @Override
    public void doBeforeComposeChildren(Component comp) throws Exception {
        super.doBeforeComposeChildren(comp);

        String strId = Executions.getCurrent().getParameter("id");
        try {
            int id = Integer.parseInt(strId);
            if (id > 0) {
                FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
                flashcardset = srv.findByID(new Long(id));
            }
        } catch (Exception e) {
        }

        if (flashcardset == null) {
            throw new OperationException("Unknow flashcardset");
        }
    }

    public FlashcardSet getFlashcardset() {
        return flashcardset;
    }

    public void setFlashcardset(FlashcardSet flashcardset) {
        this.flashcardset = flashcardset;
    }

    public List<FlashcardSet> getRelatedList() {
        FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
        return srv.getRelatedFlashcardSet(flashcardset);
    }

    public ListModelList<Flashcard> getFlashcards() {
        return new ListModelList<Flashcard>(flashcardset.getFlashcards());
    }

    public Listbox getListboxFlashcard() {
        return listboxFlashcard;
    }

    public void setListboxFlashcard(Listbox listboxFlashcard) {
        this.listboxFlashcard = listboxFlashcard;
    }

    @Listen("onSelect=#listboxFlashcard")
    public void onFlashcardSelect() {
        Flashcard card = listboxFlashcard.getSelectedItem().getValue();
        updateSelectedFlashcard(card);
    }

    private void updateSelectedFlashcard(Flashcard card) {
        // TODO require here
        if (card == null) {

        } else {

        }
    }

    @Listen("onClick=#lnkSelectAll")
    public void onSelectAll() {
        for (Checkbox chk : chkFlashcards) {
            chk.setChecked(true);
        }
        Clients.evalJavaScript("$('.box_scroll :checkbox').uniform();");
    }
}
